package com.dries.security.browser.config;

import com.dries.security.browser.logout.DriesLogoutSuccessHandler;
import com.dries.security.browser.session.DriesExpiredSessionStrategy;
import com.dries.security.browser.session.DriesInvalidSessionStrategy;
import com.dries.security.core.properties.SecurityCoreProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/9/17 18:45
 */
@Configuration
public class BrowserSecurityBeanConfig {

    @Resource
    private SecurityCoreProperties coreProperties;

    @Bean
    @ConditionalOnMissingBean(InvalidSessionStrategy.class)
    public InvalidSessionStrategy invalidSessionStrategy(){
        return new DriesInvalidSessionStrategy(coreProperties.getBrowser().getSession().getSessionInvalidUrl());
    }

    @Bean
    @ConditionalOnMissingBean(SessionInformationExpiredStrategy.class)
    public SessionInformationExpiredStrategy sessionInformationExpiredStrategy(){
        return new DriesExpiredSessionStrategy(coreProperties.getBrowser().getSession().getSessionInvalidUrl());
    }

    @Bean
    @ConditionalOnMissingBean(LogoutSuccessHandler.class)
    public LogoutSuccessHandler logoutSuccessHandler() {
        return new DriesLogoutSuccessHandler(coreProperties.getBrowser().getLogoutPage());
    }

}
